﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统字典数据仓储
/// </summary>
public class SysDictDataRep : BaseRep<SysDictDataModel>
{
    private static string sqlFields = string.Format(@"
         `dict_code`
        ,`dict_sort`
        ,`dict_label`
        ,`dict_value`
        ,`dict_type`
        ,`data_type`
        ,`css_class`
        ,`list_class`
        ,`is_default`
        ,`status`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
        ,`remark`
    ");
    /// <summary>
    /// 通过字典编码获取系统字典信息
    /// </summary>
    /// <param name="dictCode">字典编码</param>
    /// <returns>SysDictDataModel?</returns>
    public SysDictDataModel? GetSysDictDataByDictCode(long dictCode)
    {
        string sqlWhere = " and `dict_code` = @DictCode ";

        return QuerySingle(sqlFields, sqlWhere, new { DictCode = dictCode });
    }

    /// <summary>
    /// 通过字典类型获取系统字典列表数据
    /// </summary>
    /// <param name="dictType">字典类型</param>
    /// <returns>IEnumerable<SysDictDataModel></returns>
    public IEnumerable<SysDictDataModel> GetSysDictDataListByDictType(string dictType)
    {
        string sqlWhere = " and `dict_type` = @DictType order by `dict_sort` asc ";

        return QueryList(sqlFields, sqlWhere, new { DictType = dictType });
    }

    /// <summary>
    /// 获取系统字典列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysDictDataModel></returns>
    public IEnumerable<SysDictDataModel> GetSysDictDataList(SysDictDataModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.DictLabel))
        {
            sqlWhere += " and `dict_label` like concat('%', @DictLabel ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DictValue))
        {
            sqlWhere += " and `dict_value` like concat('%', @DictValue ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DictType))
        {
            sqlWhere += " and `dict_type` =  @DictType ";
        }
        if(!string.IsNullOrWhiteSpace(model.DataType))
        {
            sqlWhere += " and `data_type` = @DataType ";
        }
        if (!string.IsNullOrWhiteSpace(model.IsDefault))
        {
            sqlWhere += " and `is_default` =  @IsDefault ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取系统字典列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysDictDataModel></returns>
    public Page<SysDictDataModel> GetSysDictDataPageList(SysDictDataModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.DictLabel))
        {
            sqlWhere += " and `dict_label` like concat('%', @DictLabel ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DictValue))
        {
            sqlWhere += " and `dict_value` like concat('%', @DictValue ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DictType))
        {
            sqlWhere += " and `dict_type` =  @DictType ";
        }
        if (!string.IsNullOrWhiteSpace(model.DataType))
        {
            sqlWhere += " and `data_type` = @DataType ";
        }
        if (!string.IsNullOrWhiteSpace(model.IsDefault))
        {
            sqlWhere += " and `is_default` =  @IsDefault ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过字典编码更新系统字典
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysDictDataByDictCode(SysDictDataModel model)
    {
        string sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.DictLabel))
        {
            sqlSet += " `dict_label` = @DictLabel, ";
        }

        if (!string.IsNullOrWhiteSpace(model.DictValue))
        {
            sqlSet += " `dict_value` = @DictValue, ";
        }

        if (model.DictSort > -1)
        {
            sqlSet += " `dict_sort` = @DictSort, ";
        }

        if (!string.IsNullOrWhiteSpace(model.DictType))
        {
            sqlSet += " `dict_type` = @DictType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.DataType))
        {
            sqlSet += " `data_type` = @DataType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.CssClass))
        {
            sqlSet += " `css_class` = @CssClass, ";
        }

        if (!string.IsNullOrWhiteSpace(model.ListClass))
        {
            sqlSet += " `list_class` = @ListClass, ";
        }

        if (!string.IsNullOrWhiteSpace(model.IsDefault))
        {
            sqlSet += " `is_default` = @IsDefault, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `dict_code` = @DictCode ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过系统字典编码删除系统字典数据
    /// </summary>
    /// <param name="dictCode">系统字典编码</param>
    /// <returns>int</returns>
    public int DeleteSysDictDataByDictCode(long dictCode)
    {
        string sqlWhere = " and `dict_code` = @DictCode ";

        return Delete(sqlWhere, new { DictCode = dictCode });
    }
}

